import time
from functools import partial
import serial  # 导入模块
import serial.tools.list_ports
from PyQt5 import QtGui
from PyQt5.Qt import *
from port import Ui_MainWindow

"""
接受串口数据线程 
"""


# c1f01c0:   0001  1100 0001 1111 0000 0001 1100 0000
class SerialThreadClass(QThread):  # 步骤1.创建一个线程实例
    rev_data = pyqtSignal(list)
    rev_data9 = pyqtSignal(list)
    def __init__(self, serial_para_list):
        super().__init__()
        self.serial = serial_para_list[0]

    def run(self) -> None:  # 死循环接收UDP数据
        try:
            data = []
            num = 0
            mnum = 0
            while True:
                rev_data = self.serial.read(1)
                if len(rev_data):
                    str_data = bytesToHexString(rev_data).strip()
                    if str_data == "54":
                        if len(data) == 5:
                            num += 1
                            if num > 20:
                                print("单", data)
                                num = 0
                                self.rev_data.emit(data)
                        else:
                            mnum += 1
                            if mnum > 20:
                                print("多", data)
                                mnum = 0
                                self.rev_data9.emit(data)
                        data.clear()
                        data.append(str_data)
                    else:
                        if len(data)!=0 and data[0] == "54":
                            data.append(str_data)
                    # print("rev", bytesToHexString(rev_data), chr(int(bytesToHexString(rev_data), 16)))

        except Exception as r:
            print("run函数", r)


# bytes转16进制字符串
def bytesToHexString(bs):
    try:
        return ''.join(['%02X ' % b for b in bs])
    except Exception as result:
        print(result)


class Window(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("CCU串口软件")
        self.resize(500, 500)
        self.setupUi(self)
        self.CKList()
        self.serialPort = "COM3"
        self.BTL = 38400
        self.idx = 0
        self.tNumber = 0
        # 打开串口，并得到串口对象
        self.ser = serial.Serial(self.serialPort, self.BTL, timeout=5)
        self.pushButton.clicked.connect(self.CKList)
        self.initDataFunc()
        self.signalAndSlotFunc()
        self.serialTH = SerialThreadClass([self.ser])
        self.serialTH.rev_data.connect(self.rev_data_func)
        self.serialTH.rev_data9.connect(self.rev_data_func9)
        self.serialTH.start()
        self.t5_timer = QTimer(self)
        self.t5_timer.timeout.connect(self.T5TimerFunc)
        # self.light0.setPixmap(QtGui.QPixmap(":/img/io1.png"))
        self.light_list = []
        self.lightGroupFunc()


    def lightGroupFunc(self):
        try:
            self.light_list.append(self.light0)
            self.light_list.append(self.light1)
            self.light_list.append(self.light2)
            self.light_list.append(self.light3)
            self.light_list.append(self.light4)
            self.light_list.append(self.light5)
            self.light_list.append(self.light6)
            self.light_list.append(self.light7)
            self.light_list.append(self.light8)
            print(self.light_list)
        except Exception as r:
            print("lightGroupFunc", r)

    # 接收函数 单个指令
    def rev_data_func9(self, data):
        try:
            if self.tNumber == 9:
                if data[1] == "01":
                    self.light_list[0].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[0].setPixmap(QtGui.QPixmap(":/img/io0.png"))

                if data[2] == "01":
                    self.light_list[1].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[1].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[3] == "01":
                    self.light_list[2].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[2].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[4] == "01":
                    self.light_list[3].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[3].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[8] == "01":
                    self.light_list[4].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[4].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[12] == "01":
                    self.light_list[5].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[5].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[16] == "01":
                    self.light_list[6].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[6].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[20] == "01":
                    self.light_list[7].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[7].setPixmap(QtGui.QPixmap(":/img/io0.png"))
                if data[24] == "01":
                    self.light_list[8].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[8].setPixmap(QtGui.QPixmap(":/img/io0.png"))
            print(data)
        except Exception as r:
            print("接收函数", r)

    # 接收函数00-08指令
    def rev_data_func(self, data):
        try:
            if self.tNumber == 9:
                pass
            else:
                if data[1] == "01":
                    self.light_list[self.tNumber].setPixmap(QtGui.QPixmap(":/img/io1.png"))
                else:
                    self.light_list[self.tNumber].setPixmap(QtGui.QPixmap(":/img/io0.png"))

            print(data)
        except Exception as r:
            print("接收函数", r)


    # 指示灯初始化
    def lightInit(self):
        try:
            for i in range(9):
                self.light_list[i].setPixmap(QtGui.QPixmap(":/img/io0.png"))
        except Exception as r:
            print("指示灯初始化", r)

    # 信号与槽函数
    def signalAndSlotFunc(self):
        try:
            self.pushButton_2.clicked.connect(self.settingPortFunc)
            self.pushButton_3.clicked.connect(self.settingBTLFunc)
            self.pushButton_init.clicked.connect(self.openCKSendDataFunc)
            self.pushButton_Observer.clicked.connect(self.ObserverFunc)
            self.E003_00.clicked.connect(self.E003_00Func)
            self.E003_01.clicked.connect(self.E003_01Func)
            self.E003_02.clicked.connect(self.E003_02Func)
            self.E003_03.clicked.connect(self.E003_03Func)
            self.E003_04.clicked.connect(self.E003_04Func)
            self.E003_05.clicked.connect(self.E003_05Func)
            self.E003_06.clicked.connect(self.E003_06Func)
            self.E003_07.clicked.connect(self.E003_07Func)
            self.E003_08.clicked.connect(self.E003_08Func)
            self.pushButton_enter.clicked.connect(self.E003EnterFunc)
            self.E003_00_T5.clicked.connect(partial(self.E003_00T5Func, 0))
            self.E003_01_T5.clicked.connect(partial(self.E003_00T5Func, 1))
            self.E003_02_T5.clicked.connect(partial(self.E003_00T5Func, 2))
            self.E003_03_T5.clicked.connect(partial(self.E003_00T5Func, 3))
            self.E003_04_T5.clicked.connect(partial(self.E003_00T5Func, 4))
            self.E003_05_T5.clicked.connect(partial(self.E003_00T5Func, 5))
            self.E003_06_T5.clicked.connect(partial(self.E003_00T5Func, 6))
            self.E003_07_T5.clicked.connect(partial(self.E003_00T5Func, 7))
            self.E003_08_T5.clicked.connect(partial(self.E003_00T5Func, 8))
            self.pushButton_E003_00to07.clicked.connect(partial(self.E003_00to07Func, 9))
            self.pushButton_stop.clicked.connect(self.stopFunc)
            self.pushButton_restart.clicked.connect(self.restartFunc)
        except Exception as r:
            print("信号与槽函数", r)

    # 基础数据
    def initDataFunc(self):
        try:
            self.initData = ['F1', 'F4', '76', 'F8', '6E', '00', 'F1', 'F4', '76', 'F8', '6E', '00', 'F1', 'F4', '76',
                             'F8',
                             '6E',
                             '00', 'F1', 'F4', '8E', 'F8', '86', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '30',
                             '30',
                             '30', '31', 'F8', '0B', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '31',
                             '30',
                             '31',
                             'F8', '0C', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '32', '30', '31',
                             'F8',
                             '0D',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '33', '30', '31', 'F8', '0E',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '34', '30', '31', 'F8', '0F',
                             '00',
                             'F1',
                             'F4',
                             '49', '49',
                             '30',
                             '30', '30', '30', '30', '35', '30', '31', 'F8', '10', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '30', '36', '30', '31', 'F8', '11', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '30',
                             '37',
                             '30',
                             '31', 'F8', '12', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '38', '30',
                             '31',
                             'F8',
                             '13',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '39', '30', '31', 'F8', '14',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '30', '41', '30', '31', 'F8', '1C', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '30', '42', '30', '31', 'F8', '1D', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '30', '43', '30', '31', 'F8', '1E', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '30',
                             '44',
                             '30',
                             '31', 'F8', '1F', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '45', '30',
                             '31',
                             'F8',
                             '20',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '30', '46', '30', '31', 'F8', '21',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '31', '30', '30', '31', 'F8', '0C', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '31', '31', '30', '31', 'F8', '0D', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '31', '32', '30', '31', 'F8', '0E', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '31',
                             '33',
                             '30',
                             '31', 'F8', '0F', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '31', '33', '30',
                             '31',
                             'F8',
                             '0F',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '31', '34', '30', '31', 'F8', '10',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '31', '35', '30', '31', 'F8', '11', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '31', '36', '30', '31', 'F8', '12', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '31', '37', '30', '31', 'F8', '13', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '31',
                             '38',
                             '30',
                             '31', 'F8', '14', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '31', '39', '30',
                             '31',
                             'F8',
                             '15',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '31', '41', '30', '31', 'F8', '1D',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '31', '42', '30', '31', 'F8', '1E', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '31', '43', '30', '31', 'F8', '1F', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '31', '44', '30', '31', 'F8', '20', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '31',
                             '45',
                             '30',
                             '31', 'F8', '21', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '31', '46', '30',
                             '31',
                             'F8',
                             '22',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '31', '46', '30', '31', 'F8', '22',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '32', '30', '30', '31', 'F8', '0D', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '32', '30', '30', '31', 'F8', '0D', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '32', '31', '30', '31', 'F8', '0E', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '32',
                             '32',
                             '30',
                             '31', 'F8', '0F', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '32', '33', '30',
                             '31',
                             'F8',
                             '10',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '32', '34', '30', '31', 'F8', '11',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '32', '35', '30', '31', 'F8', '12', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '32', '35', '30', '31', 'F8', '12', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '32', '36', '30', '31', 'F8', '13', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '32',
                             '37',
                             '30',
                             '31', 'F8', '14', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '32', '38', '30',
                             '31',
                             'F8',
                             '15',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '32', '39', '30', '31', 'F8', '16',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '32', '41', '30', '31', 'F8', '1E', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '32', '42', '30', '31', 'F8', '1F', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '32', '43', '30', '31', 'F8', '20', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '32',
                             '44',
                             '30',
                             '31', 'F8', '21', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '32', '45', '30',
                             '31',
                             'F8',
                             '22',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '32', '46', '30', '31', 'F8', '23',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '33', '30', '30', '31', 'F8', '0E', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '33', '31', '30', '31', 'F8', '0F', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '33', '32', '30', '31', 'F8', '10', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '33',
                             '33',
                             '30',
                             '31', 'F8', '11', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '33', '34', '30',
                             '31',
                             'F8',
                             '12',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '33', '35', '30', '31', 'F8', '13',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '33', '36', '30', '31', 'F8', '14', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '33', '37', '30', '31', 'F8', '15', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '33', '38', '30', '31', 'F8', '16', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30',
                             '33',
                             '39',
                             '30',
                             '31', 'F8', '17', '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '33', '41', '30',
                             '31',
                             'F8',
                             '1F',
                             '00', 'F1', 'F4', '49', '49', '30', '30', '30', '30', '33', '42', '30', '31', 'F8', '20',
                             '00',
                             'F1',
                             'F4',
                             '49', '49', '30', '30', '30', '30', '33', '43', '30', '31', 'F8', '21', '00', 'F1', 'F4',
                             '49',
                             '49',
                             '30',
                             '30', '30', '30', '33', '43', '30', '31', 'F8', '21', '00', 'F1', 'F4', '49', '49', '30',
                             '30',
                             '30',
                             '30',
                             '33', '44', '30', '31', 'F8', '22', '00', 'F1', 'F4', '76', 'F8', '6E', '00', 'F1', 'F4',
                             '67',
                             'F8',
                             '5F',
                             '00', 'F1', 'F4', '73', 'F8', '6B']
            self.Observer_data = ['00', 'F1', 'F4', '67', 'F8', '5F']
            self.E00300_data0 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '43', '33', '30', '30', 'F8', '19', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data1 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '43', '36', '30', '30', 'F8', '1C', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data2 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '43', '39', '30', '30', 'F8', '1F', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data3 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '43', '43', '30', '30', 'F8', '29', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data4 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '43', '46', '30', '30', 'F8', '2C', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data5 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '44', '32', '30', '30', 'F8', '19', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data6 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '44', '35', '30', '30', 'F8', '1C', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data7 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '44', '38', '30', '30', 'F8', '1F', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_data8 = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1',
                                 'F4', '49', '42', '30', '30', '30', '30', '44', '42', '30', '30', 'F8', '29', '00',
                                 'F1', 'F4', '72', '30', '30', 'F8', 'CA']
            self.E00300_enter = ['00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '34', '49', '30', '30', 'F8',
                                 'D5']
            self.T5_data = [
                ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '31', '30', '39', '30', '39', '37', '30', '38', '45', '35', '44', '42', '30', '30', '30', '30', '30', '31', '35', '33', '35', '30', '30', '30', '35', '31', '30', '31', 'F8', '7F', '54', '54', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '32', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '31', '38', '38', '36', '30', '30', '30', '35', '31', '30', '31', 'F8', '80', '54', '54', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '33', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '32', '32', '43', '45', '30', '30', '30', '35', '31', '30', '31', 'F8', '9D', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '34', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '31', '41', '44', '33', '30', '30', '30', '35', '31', '30', '31', 'F8', 'A2', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '35', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '32', '33', '35', '31', '30', '30', '30', '35', '31', '30', '31', 'F8', '8C', '54', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '36', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '31', '38', '43', '36', '30', '30', '30', '35', '31', '30', '31', 'F8', 'AB', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '37', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '32', '32', '43', '41', '30', '30', '30', '35', '31', '30', '31', 'F8', 'B9', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '32', '30', '33', '33', '35', '37', '38', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '31', '42', '43', '42', '30', '30', '30', '35', '31', '30', '31', 'F8', 'D1', '54', '54', '54']
                , ['00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '32', '30', '33', '33', '35', '37', '39', '30', '39', '30', '39', '31', '30', '30', '45', '36', '45', '36', '38', '30', '30', '30', '30', '31', '42', '43', '42', '30', '30', '30', '35', '31', '30', '31', 'F8', 'D9', '54', '54', '54']
            ]
            self.stop_data = ['53', '43']
            self.restart_data = ['00', 'F1', 'F4', '67', 'F8', '5F']
            self.E003_00_to_07 = ['00', 'F1', 'F4', '67', 'F8', '5F', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '43', '33', '30', '30', 'F8', '19', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '43', '36', '30', '30', 'F8', '1C', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '43', '39', '30', '30', 'F8', '1F', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '43', '43', '30', '30', 'F8', '29', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '43', '46', '30', '30', 'F8', '2C', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '44', '32', '30', '30', 'F8', '19', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '44', '35', '30', '30', 'F8', '1C', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '2D', 'F8', '25', '00', 'F1', 'F4', '49', '42', '30', '30', '30', '30', '44', '38', '30', '30', 'F8', '1F', '00', 'F1', 'F4', '72', '30', '30', 'F8', 'CA', '00', 'F1', 'F4', '64', 'F8', '5C', '00', 'F1', 'F4', '34', '49', '30', '30', 'F8', 'D5', '00', 'F1', 'F4', '59', '30', '32', '30', '33', '33', '35', '37', '31', '30', '32', '30', '33', '33', '35', '37', '32', '30', '32', '30', '33', '33', '35', '37', '33', '30', '32', '30', '33', '33', '35', '37', '34', '30', '32', '30', '33', '33', '35', '37', '35', '30', '32', '30', '33', '33', '35', '37', '36', '30', '32', '30', '33', '33', '35', '37', '37', '30', '32', '30', '33', '33', '35', '37', '38', '30', '39', '30', '39', '30', '30', '46', '36', '33', '41', '42', '30', '35', '45', '33', '46', '36', '32', '34', '35', '30', '30', '30', '31', '31', '30', '38', 'F8', 'C7', '54', '54', '54', '54', '54']
        except Exception as r:
                print('基础数据', r)

    # Oberserver
    def ObserverFunc(self):
        try:
            for i in self.Observer_data:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("ObserverFunc", r)

    # E003_00
    def E003_00Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_00")
            for i in self.E00300_data0:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_00Func", r)

    # E003_01
    def E003_01Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_01")
            for i in self.E00300_data1:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_01Func", r)

    # E003_02
    def E003_02Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_02")
            for i in self.E00300_data2:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_02Func", r)

    # E003_03
    def E003_03Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_03")
            for i in self.E00300_data3:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_03Func", r)

    # E003_04
    def E003_04Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_04")
            for i in self.E00300_data4:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_04Func", r)

    # E003_05
    def E003_05Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_05")
            for i in self.E00300_data5:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_05Func", r)

    # E003_06
    def E003_06Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_06")
            for i in self.E00300_data6:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_06Func", r)

    # E003_07
    def E003_07Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_07")
            for i in self.E00300_data7:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_07Func", r)

    # E003_08
    def E003_08Func(self):
        try:
            self.lightInit()
            self.label_3.setText("E003_08")
            for i in self.E00300_data8:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003_08Func", r)

    # 回车
    def E003EnterFunc(self):
        try:
            for i in self.E00300_enter:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("E003EnterFunc", r)

    # E003_00T5Func
    def E003_00T5Func(self, idx):
        try:
            self.tNumber = idx
            self.t5_timer.start(5)
        except Exception as r:
            print("E003_00T5Func", r)

    # E003_00to07Func
    def E003_00to07Func(self, idx):
        try:
            self.lightInit()
            self.tNumber = idx
            self.t5_timer.start(5)
        except Exception as r:
            print("E003_00to07Func", r)

    # stop
    def stopFunc(self):
        try:
            for i in self.stop_data:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
            self.t5_timer.stop()
            self.idx = 0
        except Exception as r:
            print("stop", r)

    # restartFunc
    def restartFunc(self):
        try:
            for i in self.restart_data:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
            self.t5_timer.stop()
        except Exception as r:
            print("restartFunc", r)

    # T5 发送定时器函数
    def T5TimerFunc(self):
        try:
            if self.tNumber == 9:
                if self.idx < len(self.E003_00_to_07):
                    send_data = [int(self.E003_00_to_07[self.idx], 16)]
                else:
                    send_data = [int('54', 16)]
            else:
                if self.idx < len(self.T5_data[self.tNumber]):
                    send_data = [int(self.T5_data[self.tNumber][self.idx], 16)]
                else:
                    send_data = [int('54', 16)]
            self.ser.write(send_data)
            self.idx += 1
            if self.idx > 10000:
                self.idx = 1000

        except Exception as r:
            print("T5定时器函数", r)

    # 打开串口,发送数据
    def openCKSendDataFunc(self):
        try:
            for i in self.initData:
                send_data = [int(i, 16)]
                self.ser.write(send_data)
                time.sleep(0.005)
        except Exception as r:
            print("打开串口", r)

    def settingPortFunc(self):
        try:
            self.serialPort = self.comboBox.currentText()
            print(self.serialPort)
        except Exception as r:
            print("settingPortFun", r)

    def settingBTLFunc(self):
        try:
            self.BTL = int(self.comboBox_2.currentText())
            print(self.BTL)
        except Exception as r:
            print("settingBTLFunc", r)

    def CKList(self):
        try:
            port_list = list(serial.tools.list_ports.comports())
            print(port_list)
            if len(port_list) == 0:
                print('无可用串口')
            else:
                for i in range(0, len(port_list)):
                    print(port_list[i])

                    self.comboBox.addItem(str(port_list[i])[0:4])
        except Exception as r:
            print("串口列表", r)


if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)

    window = Window()
    styleFile = './style.qss'
    with open(styleFile, 'r') as f:
        window.setStyleSheet(f.read())
    window.show()

    sys.exit(app.exec_())
